package com.wwy.lease.web.admin.custom.schedule;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wwy.lease.model.entity.LeaseAgreement;
import com.wwy.lease.model.enums.LeaseStatus;
import com.wwy.lease.web.admin.service.LeaseAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * - SpringBoot中的cron表达式语法如下
 *
 *   ```
 *     ┌───────────── second (0-59)
 *     │ ┌───────────── minute (0 - 59)
 *     │ │ ┌───────────── hour (0 - 23)
 *     │ │ │ ┌───────────── day of the month (1 - 31)
 *     │ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
 *     │ │ │ │ │ ┌───────────── day of the week (0 - 7)
 *     │ │ │ │ │ │          (0 or 7 is Sunday, or MON-SUN)
 *     │ │ │ │ │ │
 *     * * * * * *
 */
@Component
public class ScheduleTasks {

    @Autowired
    private LeaseAgreementService service;

//    @Scheduled(cron = "* * * * * *")
//    public void test() {
//        System.out.println(new Date());
//    }


    @Scheduled(cron = "0 0 0 * * *")
    public void checkLeaseStatus() {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, new Date());
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        updateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.EXPIRED);
        service.update(updateWrapper);
    }
}
